﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using OpalisConsole.EndUserService;
using OpalisConsole.Views.Administration.Child_Windows;

namespace OpalisConsole.Views.Administration
{
    public partial class OSDVariableSettings : Page
    {
        public OSDVariableSettings()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!(bool)App.Current.Resources["FreshStartAdmin"])
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
            else
            {
                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.getOSDSettingsCompleted += new EventHandler<getOSDSettingsCompletedEventArgs>(proxy_getOSDSettingsCompleted_INIT);
                proxy.getOSDSettingsAsync("Default");
            }
        }

        private void boxGroupName_GotFocus(object sender, RoutedEventArgs e)
        {
            if (!this.Resources.Contains("AD_SITES"))
            {
                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.getADSitesCompleted += new EventHandler<getADSitesCompletedEventArgs>(proxy_getADSitesCompleted);
                proxy.getADSitesAsync();
            }
            else
            {
                ActiveDirectorySiteChooser siteChooser = new ActiveDirectorySiteChooser((List<String>)this.Resources["AD_SITES"]);
                siteChooser.Closed += new EventHandler(siteChooser_Closed);
                siteChooser.Show();
            }
        }

        void siteChooser_Closed(object sender, EventArgs e)
        {
            ActiveDirectorySiteChooser ADSiteChooser = (ActiveDirectorySiteChooser)sender;
            if (ADSiteChooser.DialogResult.Equals(true) && ADSiteChooser.gridSites.SelectedItem != null)
            {
                boxGroupName.Text = ADSiteChooser.gridSites.SelectedItem.ToString();
                
                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.getOSDSettingsCompleted += new EventHandler<getOSDSettingsCompletedEventArgs>(proxy_getOSDSettingsCompleted);
                proxy.getOSDSettingsAsync(ADSiteChooser.gridSites.SelectedItem.ToString());
            }
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            OSDSettings current = (OSDSettings)this.Resources["OSDSettings"];

            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
            if ((!boxOSDDataYN.Text.Equals(current.OSDDataYN)) && (!boxOSDDataYN.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text,"OSDDataYN",boxOSDDataYN.Text);
            }
            if ((!boxOSDDomainName.Text.Equals(current.OSDDomainName)) && (!boxOSDDomainName.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDDomainName", boxOSDDomainName.Text);
            }
            if ((!boxOSDDomainOUName.Text.Equals(current.ODSDomainOUName)) && (!boxOSDDomainOUName.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDDomainOUName", boxOSDDomainOUName.Text);
            }
            if ((!boxOSDNotifyEmail.Text.Equals(current.OSDNotifyEmail)) && (!boxOSDNotifyEmail.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDNotifyEmail", boxOSDNotifyEmail.Text);
            }
            if ((!boxOSDNotifyEmailCC.Text.Equals(current.OSDNotifyEmailCC)) && (!boxOSDNotifyEmailCC.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDNotifyEmailCC", boxOSDNotifyEmailCC.Text);
            }
            if ((!boxOSDTimeZone.Text.Equals(current.OSDTimeZone)) && (!boxOSDTimeZone.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDTimeZone", boxOSDTimeZone.Text);
            }
            if ((!boxOSDUILanguage.Text.Equals(current.OSDUILanguage)) && (!boxOSDUILanguage.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDUILanguage", boxOSDUILanguage.Text);
            }
            if ((!boxOSDUserLocale.Text.Equals(current.OSDUserLocale)) && (!boxOSDUserLocale.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDUserLocale", boxOSDUserLocale.Text);
            }
            if ((!boxOSDInputLocale.Text.Equals(current.OSDInputLocale)) && (!boxOSDInputLocale.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDInputLocale", boxOSDInputLocale.Text);
            }
            if ((!boxOSDSystemLocale.Text.Equals(current.OSDSystemLocale)) && (!boxOSDSystemLocale.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "OSDSystemLocale", boxOSDSystemLocale.Text);
            }
            if ((!boxSCCMSiteCode.Text.Equals(current.OSDSystemLocale)) && (!boxSCCMSiteCode.Text.Contains("Default")))
            {
                proxy.updateOSDSettingAsync(boxGroupName.Text, "SCCMSiteCode", boxSCCMSiteCode.Text);
            }
            UpdatedOSDVariableSettings varSettingsUpdated = new UpdatedOSDVariableSettings();
            varSettingsUpdated.Show();
        }

        void proxy_getADSitesCompleted(object sender, getADSitesCompletedEventArgs e)
        {
            List<String> Sites = new List<String>();

            foreach (String site in e.Result)
            {
                if (site != String.Empty)
                {
                    Sites.Add(site);
                }
            }
            Sites.Sort();
            this.Resources.Add("AD_SITES", Sites);
            ActiveDirectorySiteChooser siteChooser = new ActiveDirectorySiteChooser(Sites);
            siteChooser.Closed += new EventHandler(siteChooser_Closed);
            siteChooser.Show();
        }

        void proxy_getOSDSettingsCompleted(object sender, getOSDSettingsCompletedEventArgs e)
        {
            OSDSettings settings = (OSDSettings)e.Result;

            if (this.Resources.Contains("OSDSettings"))
            {
                this.Resources.Remove("OSDSettings");
                this.Resources.Add("OSDSettings", settings);
            }
            else
            {
                this.Resources.Add("OSDSettings", settings);
            }

            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
            proxy.getOSDSettingsCompleted += new EventHandler<getOSDSettingsCompletedEventArgs>(proxy_getOSDSettingsCompleted_DEFAULT);
            proxy.getOSDSettingsAsync("Default");
        }

        void proxy_getOSDSettingsCompleted_INIT(object sender, getOSDSettingsCompletedEventArgs e)
        {
            OSDSettings settings = (OSDSettings)e.Result;

            boxOSDDataYN.Text = settings.OSDDataYN;
            boxOSDDomainName.Text = settings.OSDDomainName;
            boxOSDDomainOUName.Text = settings.ODSDomainOUName;
            boxOSDInputLocale.Text = settings.OSDInputLocale;
            boxOSDNotifyEmail.Text = settings.OSDNotifyEmail;
            boxOSDNotifyEmailCC.Text = settings.OSDNotifyEmailCC;
            boxOSDSystemLocale.Text = settings.OSDSystemLocale;
            boxOSDTimeZone.Text = settings.OSDTimeZone;
            boxOSDUILanguage.Text = settings.OSDUILanguage;
            boxOSDUserLocale.Text = settings.OSDUserLocale;
            boxSCCMSiteCode.Text = settings.SCCMSiteCode;

            if (this.Resources.Contains("OSDSettings"))
            {
                this.Resources.Remove("OSDSettings");
                this.Resources.Add("OSDSettings", settings);
            }
            else
            {
                this.Resources.Add("OSDSettings", settings);
            }
        }

        void proxy_getOSDSettingsCompleted_DEFAULT(object sender, getOSDSettingsCompletedEventArgs e)
        {
            OSDSettings Defaultsettings = (OSDSettings)e.Result;
            OSDSettings settings = (OSDSettings)this.Resources["OSDSettings"];

            if (settings.OSDDataYN != null && !settings.OSDDataYN.Equals(String.Empty)) { boxOSDDataYN.Text = settings.OSDDataYN; }
            else { boxOSDDataYN.Text = "(Default) " + Defaultsettings.OSDDataYN; }

            if (settings.OSDDomainName != null && !settings.OSDDomainName.Equals(String.Empty)) { boxOSDDomainName.Text = settings.OSDDomainName; }
            else { boxOSDDomainName.Text = "(Default) " + Defaultsettings.OSDDomainName; }

            if (settings.ODSDomainOUName != null && !settings.ODSDomainOUName.Equals(String.Empty)) { boxOSDDomainOUName.Text = settings.ODSDomainOUName; }
            else { boxOSDDomainOUName.Text = "(Default) " + Defaultsettings.ODSDomainOUName; }

            if (settings.OSDInputLocale != null && !settings.OSDInputLocale.Equals(String.Empty)) { boxOSDInputLocale.Text = settings.OSDInputLocale; }
            else { boxOSDInputLocale.Text = "(Default) " + Defaultsettings.OSDInputLocale; }

            if (settings.OSDNotifyEmail != null && !settings.OSDNotifyEmail.Equals(String.Empty)) { boxOSDNotifyEmail.Text = settings.OSDNotifyEmail; }
            else { boxOSDNotifyEmail.Text = "(Default) " + Defaultsettings.OSDNotifyEmail; }

            if (settings.OSDNotifyEmailCC != null && !settings.OSDNotifyEmailCC.Equals(String.Empty)) { boxOSDNotifyEmailCC.Text = settings.OSDNotifyEmailCC; }
            else { boxOSDNotifyEmailCC.Text = "(Default) " + Defaultsettings.OSDNotifyEmailCC; }

            if (settings.OSDSystemLocale != null && !settings.OSDSystemLocale.Equals(String.Empty)) { boxOSDSystemLocale.Text = settings.OSDSystemLocale; }
            else { boxOSDSystemLocale.Text = "(Default) " + Defaultsettings.OSDSystemLocale; }

            if (settings.OSDTimeZone != null && !settings.OSDTimeZone.Equals(String.Empty)) { boxOSDTimeZone.Text = settings.OSDTimeZone; }
            else { boxOSDTimeZone.Text = "(Default) " + Defaultsettings.OSDTimeZone; }

            if (settings.OSDUILanguage != null && !settings.OSDUILanguage.Equals(String.Empty)) { boxOSDUILanguage.Text = settings.OSDUILanguage; }
            else { boxOSDUILanguage.Text = "(Default) " + Defaultsettings.OSDUILanguage; }

            if (settings.OSDUserLocale != null && !settings.OSDUserLocale.Equals(String.Empty)) { boxOSDUserLocale.Text = settings.OSDUserLocale; }
            else { boxOSDUserLocale.Text = "(Default) " + Defaultsettings.OSDUserLocale; }

            if (settings.SCCMSiteCode != null && !settings.SCCMSiteCode.Equals(String.Empty)) { boxSCCMSiteCode.Text = settings.SCCMSiteCode; }
            else { boxSCCMSiteCode.Text = "(Default) " + Defaultsettings.SCCMSiteCode; }
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
            proxy.getOSDSettingsCompleted += new EventHandler<getOSDSettingsCompletedEventArgs>(proxy_getOSDSettingsCompleted);
            proxy.getOSDSettingsAsync(boxGroupName.Text);
        }
    }
}
